import {
  Injectable
} from '@angular/core';

@Injectable()
export class CommonService {

  constructor() {}

  dateFormat = function (date, format) {
    var args = {
      "M+": date.getMonth() + 1,
      "d+": date.getDate(),
      "h+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds(),
      "q+": Math.floor((date.getMonth() + 3) / 3), //quarter
      "S": date.getMilliseconds()
    };
    if (/(y+)/.test(format))
      format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var i in args) {
      var n = args[i];
      if (new RegExp("(" + i + ")").test(format))
        format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));
    }
    return format;
  };

  allItemSelect = function (itemList, allItemSelected, key) {
    if (!key) {
      key = 'selected'
    }
    itemList.forEach((value, index) => {
      if (!value[key]) {
        value[key] = false;
      }
      if (value[key] != allItemSelected.all) {
        value[key] = !value[key];
      }
    })
  }
  itemSelect = function (itemList, allItemSelected, key) {
    if (!key) {
      key = 'selected'
    }
    let count = 0;
    itemList.forEach((value, index) => {
      if (value[key]) {
        count++;
      }
    })
    if (count == itemList.length) {
      allItemSelected.all = true;
    } else {
      allItemSelected.all = false;
    }
  }
}
